package ai.people.netmon.warning.service.impl;

import ai.people.core.grpc.lib.es.*;
import ai.people.core.grpc.utils.ProtoJsonUtil;
import ai.people.netmon.framework.domain.warning.entity.WarningLabelTree;
import ai.people.netmon.framework.domain.warning.request.InfoWarningRequest;
import ai.people.netmon.framework.domain.warning.vo.MessageContentVO;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.utils.localdateUtil.DateTimeFormatterUtil;
import ai.people.netmon.warning.dict.DictGrpcClient;
import ai.people.netmon.warning.service.WarningInfoService;
import ai.people.netmon.warning.service.WarningLabelTreeService;
import ai.people.netmon.warning.service.WarningSpeechTemplateService;
import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 信息预警服务
 *
 * @author yuanqinglong
 * @date 2022/7/27 18:19
 */
@Service
@RequiredArgsConstructor
public class WarningInfoServiceImpl implements WarningSpeechTemplateService<InfoWarningRequest, MessageContentVO>, WarningInfoService {

    private final DictGrpcClient dictGrpcClient;

    private final WarningLabelTreeService warningLabelTreeServiceImpl;

    @GrpcClient(value = "NM-SERVICE-SEARCH-MSG")
    private BaseSearchServiceGrpc.BaseSearchServiceBlockingStub searchServiceBlockingStub;

    @GrpcClient(value = "NM-SERVICE-SEARCH-MSG")
    private WarningMessageServiceGrpc.WarningMessageServiceBlockingStub warningMessageServiceBlockingStub;


    /**
     * 搜索查询
     *
     * @param param@return {@link PageResponse}<{@link ?}>
     */
    @Override
    public PageResponse<MessageContentVO> searchQuery(InfoWarningRequest param) {
        WarningMessageGrpcRequest.Builder newBuilder = WarningMessageGrpcRequest.newBuilder();
        if (Objects.nonNull(param.getStartTime())) {
            newBuilder.setStartDate(DateTimeFormatterUtil.localDateTimeFormatterToDateTime(param.getStartTime()));
        }
        if (Objects.nonNull(param.getEndTime())) {
            newBuilder.setEndDate(DateTimeFormatterUtil.localDateTimeFormatterToDateTime(param.getEndTime()));
        }
        newBuilder.setLabel(Strings.nullToEmpty(param.getLabel()));
        newBuilder.setPlatform(Strings.nullToEmpty(param.getPlatform()));
        newBuilder.setPageNo(param.getPageNo());
        newBuilder.setPageSize(param.getPageSize());
        //  查询预警信息（有标签的信息）
        SearchPageResponse searchPageResponse = warningMessageServiceBlockingStub.warningMessage(newBuilder.build());
        // 查询所有标签、翻译信息的标签
        Map<String, WarningLabelTree> warningLabelTreeMap = warningLabelTreeServiceImpl.allLabelValueMapping();
        // 查询所有情感字典、翻译信息的情感
        Map<String, String> emotionMaps = dictGrpcClient.getDictMap("warning_message_emotion");
        List<MessageContentVO> messageContentVOList = searchPageResponse.getListList().stream()
                .map(searchResponseData -> this.objectConversion(searchResponseData, emotionMaps, warningLabelTreeMap))
                .collect(Collectors.toList());
        return PageResponse.builder(messageContentVOList, searchPageResponse.getTotal());
    }


    /**
     * 详细信息
     *
     * @param platform   平台
     * @param id         id
     * @param resultType 结果类型
     * @return {@link Object}
     */
    @Override
    public Object messageDetail(String platform, String id, Class<?> resultType) {
        SearchDetailGrpcRequest detailGrpcRequest = SearchDetailGrpcRequest.newBuilder().setPlatform(platform).setId(id).build();
        SearchDetailGrpcResponse searchDetailGrpcResponse = searchServiceBlockingStub.searchDetail(detailGrpcRequest);
        return ProtoJsonUtil.toJava(searchDetailGrpcResponse, resultType);
    }
}
